/* import React, { useEffect } from 'react'
import { useHistory, useLocation } from 'react-router-dom'

function yztoken(Com) { //接收一个组件为参数

    return function token(props) {

        let history = useHistory()
        let location = useLocation()
        //在初始和更新周期中
        useEffect(() => {
            if (location.pathname !== '/login') {
                let token = localStorage.getItem('token')
                if (!token) {
                    history.push('/login')
                }
            }
        }, [])
        return (
            <>
                <Com {...props} />
            </>
        )
    }
}

export default yztoken
 */


/* import React, { useEffect } from 'react'
import { useHistory, useLocation } from 'react-router-dom'

function yztoken(Com) {
    return function token(props) {
        let history = useHistory()
        let location = useLocation()
        useEffect(() => {
            if (location.pathname !== '/login') {
                let token = localStorage.getItem('token')
                if (!token) {
                    history.push('/login')
                }
            }

        }, [])

        return (
            <>
            <Com {...props}/>
            </>
        )
    }
}

export default yztoken
 */

import React, { useEffect } from 'react'
import { useHistory, useLocation } from 'react-router-dom'

function yztoken(Com) { //接收一个组件为参数

    return function token(props) { //返回一个加强后的新组件

        let history = useHistory()
        let location = useLocation()
        useEffect(() => {
            if (location.pathname !== '/login') {
                let token = localStorage.getItem('token')
                if (!token) {
                    history.push('/login')
                }
            }
        }, [])
        return (
            <>
                <Com {...props} />
            </>
        )
    }
}

export default yztoken



